/*
 * @Author: song
 * @Description:持久化保存单元
 * @Date: 2024年04月14日 星期日 17:04:04
 * @Modify:
 */

using System;
using System.IO;

namespace Function.Preferences
{
    public enum SPrefType
    {
        SPreInt = 1,
        SPreFloat = 2,
        SPreLong = 3,
        SPreDouble = 4,
        SPreString = 5,
    }

    [Serializable]
    public class PrefObject
    {
        public int IntValue { get; set; }

        public float FloatValue { get; set; }

        public long LongValue { get; set; }

        public double DoubleValue { get; set; }

        public string StringValue { get; set; }

        public SPrefType _sPrefType { get; set; }

        public PrefObject(BinaryReader binaryReader)
        {
            LoadPrefObject(binaryReader);
        }

        public PrefObject(SPrefType sPrefType)
        {
            _sPrefType = sPrefType;
        }

        public virtual void LoadPrefObject(BinaryReader binaryReader)
        {
            _sPrefType = (SPrefType)binaryReader.ReadByte();

            if (_sPrefType == SPrefType.SPreInt)
            {
                IntValue = binaryReader.ReadInt32();
            }
            else if (_sPrefType == SPrefType.SPreFloat)
            {
                FloatValue = binaryReader.ReadSingle();
            }
            else if (_sPrefType == SPrefType.SPreLong)
            {
                LongValue = binaryReader.ReadInt64();
            }
            else if (_sPrefType == SPrefType.SPreDouble)
            {
                DoubleValue = binaryReader.ReadDouble();
            }
            else if (_sPrefType == SPrefType.SPreString)
            {
                StringValue = binaryReader.ReadString();
            }
        }

        public virtual void SavePrefObject(BinaryWriter binaryWriter)
        {
            binaryWriter.Write((byte)_sPrefType);
            if (_sPrefType == SPrefType.SPreInt)
            {
                binaryWriter.Write(IntValue);
            }
            else if (_sPrefType == SPrefType.SPreFloat)
            {
                binaryWriter.Write(FloatValue);
            }
            else if (_sPrefType == SPrefType.SPreLong)
            {
                binaryWriter.Write(LongValue);
            }
            else if (_sPrefType == SPrefType.SPreDouble)
            {
                binaryWriter.Write(DoubleValue);
            }
            else if (_sPrefType == SPrefType.SPreString)
            {
                binaryWriter.Write(StringValue);
            }
        }
    }
}